// 获取应用实例
const app = getApp()
const api = app.globalData.api


Page({
    data: {
        info: {},
        quality: {},
        quarantine: {},
        reason: {},
        system: {},
        calendar: false,
        nowDate: new Date().getTime(),
        popup: false,
        total: 0,
        count: 0
    }, main: async function () {
        
    }, // 更新本地数据
    syncStorage: async function (id) {
        let that = this, reasonData = {}
        let res = await api.curlPost('?cla=materialInspection&fun=detail', {id: id})
        if (res.code === 1) {
            const resp = res.data
            that.setData({
                total: api.total(reasonData, ['price', 'num']),
                count: api.count(reasonData),
                info: resp.info,
                quality: resp.quality,
                quarantine: resp.quarantine,
                reason: resp.reason,
                system: resp.system,
                detail: resp.reason.length > 0 ? resp.reason : wx.getStorageSync('reasonData')
            })
        }
    }, onLoad: async function (options) {
        let data = api.jsonToArray(options.data)
        let that = this, id = data.id ? data.id : ''
        if (id) {
            await that.syncStorage(id)
        } else {
            wx.removeStorageSync('inStockInfo')
            wx.removeStorageSync('reasonData')
        }
        await this.main()
    }, 
    onUnload: async function () {
      wx.removeStorageSync('inStockInfo')
      wx.removeStorageSync('reasonData')
    },
    // 刷新物料信息
    onShow: async function () {
        const that = this
        const reasonData = wx.getStorageSync('reasonData')
        that.setData({
            // total: api.total(reasonData, ['price', 'num']),
            // count: api.count(reasonData),
            detail: reasonData,
             ['info.passNum']: Number(that.data.info.num) - api.total(reasonData, 'reasonNum')
        })
    }, onDisplay() {
        this.setData({
            calendar: true
        })
    }, // 关闭
    onClose: function (e) {
        let obj = e.currentTarget.dataset
        if (obj) {
            this.setData({[obj.target]: false})
        }
    }, goto(e) {
        return api.goto(e)
    }, funRouting(type, detail) {
        switch (type) {
            case 'orderDate':
                return api.formatDate(detail)
            case 'targetName':
                const array = detail.value
                this.setData({
                    ['info.targetId']: array['value']
                })
                return array['text']
        }
    }, // 确认选择
    onConfirm: function (e) {
        let obj = e.currentTarget.dataset
        let prefix = obj.prefix ? obj.prefix : 'info'
        let field = (prefix ? prefix + '.' : '') + obj.field
        this.setData({
            [obj.target]: false, [field]: this.funRouting(obj.field, e.detail),
        })
    }, showPopup: function () {
        this.setData({
            popup: true
        });
    }, onDelete(obj) {
        const id = obj.currentTarget.dataset.id
        const {position, instance} = obj.detail;
        const reasonData = wx.getStorageSync('reasonData')
        switch (position) {
            // case 'left':
            // case 'cell':
            //     instance.close();
            //     break;
            case 'right':
                wx.showModal({
                    title: '提示', content: '确定删除吗？', success: (res) => {
                        if (res.confirm) {
                            delete reasonData[id]
                            wx.setStorageSync('reasonData', reasonData)
                            instance.close()
                            this.setData({detail: reasonData})
                            api.showToast('删除成功')
                        }
                    },
                })
                break;
        }
    }, passNum(e) {
        this.setData({
            ['info.passNum']: e.detail,
        })
    }, formSubmit: async function (e) {
        let obj = e.detail.value
        let reasonData = wx.getStorageSync('reasonData')
        let reasonDataArray = Object.values(reasonData)
        reasonDataArray.forEach(function (item) {
            item['id'] = item['id'] ? item['id'] : api.suiji()
            delete item['text']
        })
        obj['detail'] = api.arrayToJson(reasonDataArray)
        if (obj) {
            let res = await api.curlPost('?cla=materialInspection&fun=detailEdit', obj)
            if (res.code === 1) {
                wx.navigateTo({
                    url: 'index?id=' + res.data
                })
                wx.removeStorageSync('reasonData')
                api.showToast('新增成功', 'success')
            } else {
                api.showToast(res.warn)
            }
        }
    }
});
